package db

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	log "github.com/sirupsen/logrus"
	"src/config"
)

type DataBaseWorker struct {
	Dsn      string
	Db       *sql.DB
	UserInfo userTB
}
type userTB struct {
	Id   int
	Name sql.NullString
	Age  sql.NullInt64
}

func GetMysqlOrm() *gorm.DB {
	sqlConfig := config.GetConfigInstance().Mysql
	dsnStr := sqlConfig.Name + ":" + sqlConfig.Password + "@tcp(" + sqlConfig.Address + ")/" + sqlConfig.Database + "?charset=utf8mb4&parseTime=True&loc=Local"
	dbw := DataBaseWorker{
		Dsn: dsnStr,
	}
	db, e := gorm.Open("mysql", dbw.Dsn)
	if e != nil {
		log.Warn("db error:", e.Error())
	}
	// 设置最大空闲链接数
	db.DB().SetMaxIdleConns(sqlConfig.MaxIdle)
	// 设置最大连接数
	db.DB().SetMaxOpenConns(sqlConfig.MaxContent)
	return db
}
